package org.ww.order.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.*;

import org.ww.commonutils.JwtUtils;
import org.ww.commonutils.R;
import org.ww.order.bean.Order;
import org.ww.order.service.OrderService;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author ww
 * @since 2020-11-11
 */
@RestController
@RequestMapping("/order/order")
//@CrossOrigin
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     * 生成订单
     * @param courseId
     * @param request
     * @return
     */
    @PostMapping("/addOrder/{courseId}")
    public R addOrder(@PathVariable String courseId, HttpServletRequest request){
        //添加订单，返回订单id(订单id不是主id,而是生成的一个id)
        String orderId = orderService.addOrder(JwtUtils.getMemberIdByJwtToken(request),courseId);
        return R.ok().data("orderId",orderId);
    }

    /**
     * 根据订单id查询订单信息
     * @param orderNo
     * @return
     */
    @GetMapping("/getOrderInfo/{orderNo}")
    public R getOrderInfo(@PathVariable String orderNo){
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("order_no",orderNo);
        Order order = orderService.getOne(wrapper);
        return R.ok().data("orderInfo",order);
    }

    /**
     * 根据课程id和用户id查询该课程是否被购买
     * @param courseId
     * @param memberId
     * @return
     */
    @GetMapping("/isBuyCourse/{courseId}/{memberId}")
    public boolean isBuyCourse(@PathVariable String courseId,@PathVariable String memberId){
        return orderService.isBuyCourse(courseId,memberId);
    }


}

